package com.example.springboot.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * spring-session利用redis存储session的测试
 *
 * @Author:ChenZhangKun
 * @Date: 2021/5/24 16:04
 */
@RestController
public class SpringSessionController {
    @GetMapping("/session")
    public String session(@RequestParam("key") String key, @RequestParam("value") String value, HttpSession session) {
        // 存储键值对
        // 此时会存到redis实现session键值对的共享
        session.setAttribute(key, value);
        // 拿到
        Object attribute = session.getAttribute(key);
        System.out.println(attribute);
        return "存储成功";
    }
    @GetMapping("/sessionGet")
    public String getSessionName(HttpSession session){
        String name = (String) session.getAttribute("name");
        return name;
    }
}
